package cn.syc.struck.demo;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import cn.syc.struck.demo.base.BaseActivity;
import cn.syc.struck.demo.fragment.ItemListDialogFragment;
import cn.syc.struck.demo.fragment.dialog.UpdateDialog;
import cn.syc.struck.demo.http.entity.gen.MePager;
import cn.syc.struck.demo.http.service.BlogService;
import rx.Subscriber;

public class MainActivity extends BaseActivity
        implements NavigationView.OnNavigationItemSelectedListener, ItemListDialogFragment.Listener {

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void afterCreate(Bundle savedInstanceState) {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();


                //int s=Integer.parseInt("ssss");

                //ItemListDialogFragment3.newInstance(30).show(getSupportFragmentManager(), "dialog");
                //Intent it=new Intent(MainActivity.this,NavigationActivity.class);
                //startActivity(it);
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

        drawer.addDrawerListener(toggle);
        //drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onItemClicked(int position) {

        String[] items = {"Show Fullscreen Activity", "Show Login Activity", "Show Navigation Activity",
                "Show Scrooling Activity", "Show Settings Activity", "Show Tabs Activity",
                "Grid Dialog Fragment.2", "Grid Dialog Fragment.3"
        };

        if (position == 0) {
            Intent it = new Intent(MainActivity.this, FullscreenActivity.class);
            startActivity(it);
        }
        //------------
        else if (position == 1) {
            Intent it = new Intent(MainActivity.this, LoginActivity.class);
            startActivity(it);
        } else if (position == 2) {
            Intent it = new Intent(MainActivity.this, NavigationActivity.class);
            startActivity(it);
        } else if (position == 3) {
            Intent it = new Intent(MainActivity.this, ScrollingActivity.class);
            startActivity(it);
        } else if (position == 4) {
            Intent it = new Intent(MainActivity.this, SettingsActivity.class);
            startActivity(it);
        } else if (position == 5) {
            Intent it = new Intent(MainActivity.this, TabsActivity.class);
            startActivity(it);
        } else if (position == 6) {
            Intent it = new Intent(MainActivity.this, BasicActivity.class);
            startActivity(it);
        } else if (position == 7) {
            Intent it = new Intent(MainActivity.this, CollapsingToolbarLayoutActivity.class);
            startActivity(it);
        } else if (position == 8) {
            Intent it = new Intent(MainActivity.this, LinearActivity.class);
            startActivity(it);
        } else if (position == 9) {
            final UpdateDialog dialog = UpdateDialog.newInstance();
            //dialog.show(getChildFragmentManager(),"dialog");//在v4.Fragment中用法
            dialog.show(getSupportFragmentManager(), "dialog");
            dialog.setCancelable(false);
            dialog.setListener(new UpdateDialog.OnClickListener() {
                @Override
                public void click(int position) {
                    Toast.makeText(MainActivity.this
                            , "test:" + position, Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                }
            });
        } else if (position == 10) {


            Subscriber callback = new Subscriber<MePager>() {
                @Override
                public void onCompleted() {
                }

                @Override
                public void onError(Throwable e) {
                    Toast.makeText(App.getInstance(), e.getMessage(), Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onNext(MePager t) {
                    Toast.makeText(App.getInstance(), "总页数:" + t.getTotalPage(), Toast.LENGTH_SHORT).show();
                }
            };

            BlogService.getInstance().jsonpager(1, callback);


        }
        if (position == 100) {
            Toast.makeText(MainActivity.this, "fragment" + position, Toast.LENGTH_SHORT).show();
        } else if (position == 110) {
            Toast.makeText(MainActivity.this, "fragment" + position, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Intent it = new Intent(MainActivity.this, SettingHostActivity.class);
            startActivity(it);
            return true;
        }
        //----------
        else if (id == R.id.action_sheetdialog) {
            ItemListDialogFragment.newInstance().show(getSupportFragmentManager(), "dialog");
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}
